package com.wxzz.elearing.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wxzz.elearing.course.entity.TrainPlan;
import com.wxzz.elearing.course.mapper.TrainPlanMapper;
import com.wxzz.elearing.course.service.ITrainPlanService;
import com.wxzz.elearing.course.vo.TrainPlanVo;
import com.wxzz.elearning.common.exception.ServiceException;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author
 * @since 2021-05-19
 */
@Service
public class TrainPlanServiceImpl extends ServiceImpl<TrainPlanMapper, TrainPlan> implements ITrainPlanService {

    @Resource
    private TrainPlanMapper trainPlanMapper;

    public ReturnVO saveTrainPlan(TrainPlan trainPlan) {
        if(trainPlan.getId()==null||trainPlan.getId()==0){
            trainPlanMapper.insert(trainPlan);
            return new ReturnVO("培训计划成功",1);
        }else{
            trainPlanMapper.updateById(trainPlan);
            return new ReturnVO("修改培训计划成功",1);
        }
    }

    public ReturnVO selectTrainPlanList(TrainPlanVo trainPlan){
        PageHelper.startPage(trainPlan.getCurPage(), trainPlan.getMaxPage());
        QueryWrapper queryWrapper = new QueryWrapper();
        if(StringUtils.isNotBlank(trainPlan.getTrainPlanName())){
            queryWrapper.like("train_plan_name",trainPlan.getTrainPlanName()+"%");
        }
        queryWrapper.eq("enterprise_id",trainPlan.getEnterpriseId());
        queryWrapper.orderByDesc("create_time");
        List<TrainPlan> list = trainPlanMapper.selectList(queryWrapper);
        return new ReturnVO("培训计划列表查询成功！",new JgPageVo(new PageInfo<>(list)));
    }

    public ReturnVO deleteTrainPlan(TrainPlan trainPlan){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",trainPlan.getId());
        int i = trainPlanMapper.deleteById(trainPlan.getId());
        return new ReturnVO("删除培训计划成功",null);
    }

    public ReturnVO trainPlanById(Long id){
        TrainPlan trainPlan = trainPlanMapper.selectById(id);
        return new ReturnVO("查询详情成功",trainPlan);
    }
}
